package trabajoISW2.domain.implementations;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import trabajoISW2.domain.contracts.ISubject;
import trabajoISW2.domain.exceptions.WrongIdException;
import trabajoISW2.domain.exceptions.WrongNameException;

@Entity
public class Subject implements ISubject{

	@Id @GeneratedValue(strategy = GenerationType.AUTO) 
	private int subjectId;
	private String name;
	
	@OneToMany (mappedBy = "subject", cascade = {CascadeType.MERGE, CascadeType.REFRESH})
	private List<Book> books = new LinkedList<Book>();
	
	public Subject(int subjectId,String name,List<Book> listBooks){
		this.subjectId = subjectId;
		this.name = name;
		this.books = listBooks;
	}
	
	public Subject(int subjectId,String name){
		this.subjectId = subjectId;
		this.name = name;
	}
	
	public Subject(){
	}
	
	public Subject(String name, List<Book> books) {
		this.name = name;
		this.books = books;
	}

	@Override
	public void setSubjectId(int subjectId) {
		if(subjectId<0 |subjectId>Integer.MAX_VALUE)
			throw new WrongIdException();
		else
			this.subjectId = subjectId;
	}

	@Override
	public int getSubjectId() {
		return this.subjectId;
	}

	@Override
	public void setName(String name) {
		if(name == null | name == "")
			throw new WrongNameException();
		else
			this.name = name;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return this.name;
	}

	@Override
	public void setBooks(List<Book> books) {
		if(books == null)
			throw new IllegalArgumentException();
		else
			this.books = books;
	}

	@Override
	public List<Book> getBooks() {
		// TODO Auto-generated method stub
		return this.books;
	}

	@Override
	public void addBook(Book book) {
		if(book == null)
			throw new IllegalArgumentException();
		else
			this.books.add(book);
	}

	@Override
	public void removeBook(Book book) {
		if(book == null)
			throw new IllegalArgumentException();
		else
			this.books.remove(book);
	}
	
	public boolean equals (Object o){
		boolean b = false;
		Subject s = (Subject) o;
		if (getSubjectId() == s.getSubjectId())
			b = true;
		return b;
	}


}
